package service

import (
	"context"
	"errors"
	"xs.jsjb/internal/app/user/entity"
	"xs.jsjb/pkg/utils/hash"
	"xs.jsjb/pkg/utils/jwt"
)

// Login 登陆
func (u *userService) Login(ctx context.Context, signIn *entity.LoginReq) (token string, err error) {
	var user *entity.User
	// 获取用户信息
	if user, err = u.repo.GetUserByName(ctx, signIn.Username); err != nil {
		errors.New("没有找到该用户")
		return
	}

	// 验证密码
	if user.Password != hash.MD5String(signIn.Password) {
		err = errors.New("密码不正确")
		return
	}

	// 生成token
	return jwt.GenToken(user.UserID)
}
